export class WS {
	constructor(index) {
		this.index = index
		uni.connectSocket({
		  url: 'wss://jiaoyou2.10pay.cn/wss'
		})
	}
	open = function(callback) {
		uni.onSocketOpen(function () {
			console.log('socket 已连接');
			if (typeof callback == 'function') {
				callback();
			}
		});
	}

	// 发送数据
	send = function(msg) {
		console.log('msg: ' + msg);
		uni.sendSocketMessage({
			data: msg
		})
	}

	// 接收数据
	get = function(callback) {
		uni.onSocketMessage(function (evt) {
			// console.log('index'+_this.index+ ' : '+evt.data)
			var received_msg = evt.data;
			if (typeof callback == 'function') {
				if (uni.getStorageSync('uid')) {
					callback(received_msg);
				}
			}
		});
	}
	// 关闭socket 
	close = function() {
		uni.closeSocket();
	}

}
